package com.iap.client.mediator
{
	import com.iap.app.AppGlobal;
	import com.iap.app.AppText;
	import com.iap.client.event.FillerEvent;
	import com.iap.client.event.SampleLayerChangeEvent;
	import com.iap.client.event.SampleRendererEvent;
	import com.iap.client.model.SampleFillerWithFillers;
	import com.iap.client.view.control.FillerSelectControl;
	import com.iap.client.view.favorite.FavoriteEditComboWindow;
	import com.iap.common.AppSession;
	import com.iap.common.event.FavoriteEvent;
	import com.iap.common.event.SampleEvent;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Favorite;
	import com.iap.common.model.Filler;
	import com.iap.common.model.InstanceFavorite;
	import com.iap.common.model.Sample;
	import com.iap.common.model.SampleFiller;
	import com.iap.common.signal.FavoriteRequestSignal;
	import com.iap.common.signal.FillerRequestSignal;
	import com.iap.common.signal.InstanceFavoriteRequestSignal;
	import com.iap.common.signal.SampleRequestSignal;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	public class FavoriteEditComboWindowMediator extends BaseMediator
	{
		[Inject]
		public var view:FavoriteEditComboWindow;
		[Inject]
		public var fillerRequest:FillerRequestSignal;
		[Inject]
		public var instanceFavoriteRequest:InstanceFavoriteRequestSignal;
		
		private var fillers:Array = [];
		private var fillerControl:FillerSelectControl;

		override public function onRegister():void
		{
			addContextListener(FillerEvent.RESPONSE, fillerResponseHandler);
			addContextListener(FavoriteEvent.RESPONSE, favoriteResponseHandler);
			view.layerControl.addEventListener(SampleLayerChangeEvent.CHANGE, sampleLayerChangeHandler);
			view.sampleList.addEventListener(SampleRendererEvent.SAVE_FAVORITE, sampleRendererSaveHandler);
			dispatchRequest();
		}
		
		private function favoriteResponseHandler(evt:FavoriteEvent):void
		{
			if(evt.response.task == FavoriteRequestSignal.SAVE)
			{
				Alert.show(AppText.SAMPLE_FAVORITE_SAVE_SUCCESS);
			}
		}

		private function dispatchRequest():void
		{
			AppGlobal.startWait();
			fillerRequest.dispatch(new Filler(), FillerRequestSignal.GET);
		}

		private function fillerResponseHandler(evt:FillerEvent):void
		{
			AppGlobal.endWait();
			fillers = evt.response.result as Array;
			initFillSelector(view.layerControl.dataProvider.getItemAt(0) as SampleFillerWithFillers);
		}
		
		private function sampleLayerChangeHandler(evt:SampleLayerChangeEvent):void
		{
			initFillSelector(evt.data);
		}
		
		private function sampleRendererSaveHandler(evt:SampleRendererEvent):void
		{
			var favorite:Favorite = view.model;
			favorite.sample = evt.data;

			var instanceFavorite:InstanceFavorite = new InstanceFavorite();
			instanceFavorite.instanceId = AppSession.currentInstance.id;
			instanceFavorite.favorite = favorite;
			
			instanceFavoriteRequest.dispatch(instanceFavorite, InstanceFavoriteRequestSignal.SAVE);
		}

		private function initFillSelector(item:SampleFillerWithFillers):void
		{
			fillerControl = new FillerSelectControl();
			fillerControl.fillers = fillers;
			fillerControl.layer = item.sampleFiller.layer;
			fillerControl.selectedFillers = item.fillers;
			PopUpManager.addPopUp(fillerControl, view.root, true);
			PopUpManager.centerPopUp(fillerControl);
			fillerControl.addEventListener(CloseEvent.CLOSE, closeHandler);
			view.root.addEventListener(Event.RESIZE, resizeHandler);
		}
		
		private function resizeHandler(evt:Event):void
		{
			PopUpManager.centerPopUp(fillerControl);
		}
		
		private function closeHandler(evt:Event):void
		{
			view.root.removeEventListener(Event.RESIZE, resizeHandler);
			fillerControl.removeEventListener(CloseEvent.CLOSE, closeHandler);
			var layer:int = fillerControl.layer;
			var list:Array = fillerControl.selectedFillers;
			setFillerByLayer(list, layer);
		}
		
		private function setFillerByLayer(fillers:Array, layer:int):void
		{
			var list:ArrayCollection = view.layerControl.dataProvider as ArrayCollection;
			var item:SampleFillerWithFillers;
			for(var idx:uint = 0; idx<list.length; idx++)
			{
				item = list[idx] as SampleFillerWithFillers;
				if(item.sampleFiller.layer == layer)
				{
					item.fillers = fillers;
					view.layerControl.dataProvider[idx] = item;
				}
			}
			setSampleList();
		}
		
		private function setSampleList():void
		{
			var list:Array = getFillerList();
			var result:Array = [];
			var sample:Sample = view.model.sample;
			for each(var fillers:Array in list)
			{
				var layer:uint = 1;
				var sampleFillers:Array = [];
				for each(var filler:Filler in fillers)
				{
					var sampleFillerOne:SampleFiller = new SampleFiller();
					sampleFillerOne.filler = filler;
					sampleFillerOne.layer = layer++;
					sampleFillerOne.sampleId = sample.id;
					sampleFillers.push(sampleFillerOne);
				}
				
				var item:Sample = new Sample();
				item.id = sample.id;
				item.vectorModel = sample.vectorModel;
				item.sampleFillers = sampleFillers;
				item.name = "";
				result.push(item);
			}
			view.sampleList.dataProvider = new ArrayCollection(result);
		}

		private function getFillerList():Array
		{
			var result:Array = [];
			var currentIndex:uint = 0;
			var listLayers:Array = view.layerControl.dataProvider.toArray();
			var list:Array = listLayers[currentIndex].fillers;
			for(var i:uint = 0; i<list.length; i++)
			{
				var newItem:Array = [];
				newItem.push(list[i]);
				var nextIndex:uint = currentIndex + 1;
				if(listLayers[nextIndex].fillers.length > 0)
				{
					setRecureList(newItem, nextIndex, listLayers, result);
				} else {
					result.push(newItem);
				}
			}
			return result;
		}
		
		private function setRecureList(newItem:Array, currentIndex:uint, list:Array, result:Array):void
		{
			var currentList:Array = list[currentIndex].fillers;
			for (var i:uint = 0; i < currentList.length; i++)
			{
				var newOne:Array = newItem.slice();
				newOne.push(currentList[i]);
				if (currentIndex < list.length - 1)
				{
					var nextIndex:uint = currentIndex + 1;
					setRecureList(newOne, nextIndex, list, result);
				} else
				{
					result.push(newOne);
				}
			}
		}
		
		private function getSampleFillerList(remainingList:Array, sampleFiller:SampleFiller):Array
		{
			var list:Array = [];
			for (var i:uint = 0; i<remainingList.length; i++)
			{
				var sfw:SampleFillerWithFillers = remainingList[i];
				var item:SampleFiller = new SampleFiller();
				item.filler = sfw.fillers[0];
				item.layer = sampleFiller.layer;
				item.sampleId = sampleFiller.sampleId;
				list.push(item);
			}
			return list;
		}
	}
}